package com.ysxm.util;

import java.util.Date;
import java.util.Properties;
import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtils {
	public static void sendMail(String email, String emailMsg) throws AddressException, MessagingException {
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");// 定义用户发邮件的协议，SMTP
		props.setProperty("mail.host", "smtp.qq.com");// 用户的邮件发送到SMTP邮件服务器
		props.setProperty("mail.smtp.port", "25");//设置端口
		props.setProperty("mail.smtp.auth", "true");// 指定验证为true

		// 创建验证器
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("2357786922@qq.com", "grzgcrpcrpyfeaaj");
			}
		};
        
		Session session = Session.getInstance(props, auth);

		// 创建一个message,它相当于是邮件内容
		Message message = new MimeMessage(session);

		message.setFrom(new InternetAddress("2357786922@qq.com"));// 设置发送者
		message.setRecipient(RecipientType.TO, new InternetAddress(email));// 设置发送方式与接收者
		message.setSubject("修改密码邮箱验证");//设置邮件标题
		message.setSentDate(new Date());//设置发送时间
		message.setContent(emailMsg, "text/html;charset=utf-8");//设置邮件文本内容
		//message.setText(VerificationCode.getNumeral());//参数为一个生成随机6位数验证码的String类型静态方法
		// 创建 Transport用于将邮件发送
		Transport.send(message);
	}
}
